package org.grow.present.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.grow.present.domain.Company;
import org.grow.present.dto.Msg;
import org.grow.present.exception.MissTargetException;
import org.grow.present.interceptor.AuthInterceptor;
import org.grow.present.mapper.CompanyMapper;
import org.grow.present.service.BaiduAIService;
import org.grow.present.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.Map;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyMapper companyMapper;

    @GetMapping("/find/current")
    public Msg<Company> findCurrent() {
        Integer UID = AuthInterceptor.getUID();
        Company company = companyMapper.selectById(UID);
        if (company == null) {
            throw new MissTargetException("未找到" + UID);
        }
        return Msg.success().data(company);
    }

    @PutMapping("/modify/current")
    public Msg<Company> modifyCurrent(@RequestBody Company company) {
        System.out.println(company);
        Integer UID = AuthInterceptor.getUID();
        Company companyDB = companyMapper.selectById(UID);
        companyDB.setComEmail(company.getComEmail());
        companyDB.setComName(company.getComName());
        companyDB.setComPhone(company.getComPhone());
        companyMapper.updateById(companyDB);
        return Msg.success().data(companyDB);
    }

    @Value("${present.common.image-location}")
    private String destPath;
    @Autowired
    private BaiduAIService baiduAIService;

    @Autowired
    private ObjectMapper objectMapper;

    //    company/face-reg
    @PostMapping(value = "/face-reg", consumes = {"multipart/form-data"})
    public Msg<Map> faceReg(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        System.out.println(multipartFile.getOriginalFilename());
        multipartFile.transferTo(Path.of(destPath, multipartFile.getOriginalFilename()));
        System.out.println("人脸注册");
        Integer uid = AuthInterceptor.getUID();
        Map map = baiduAIService.faceReg(multipartFile.getBytes(), uid + "");
        return Msg.success().data(map);
    }

    @PostMapping(value = "/face-search", consumes = {"multipart/form-data"})
    public Msg<Map> faceSearch(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        System.out.println("人脸认证");
        Integer uid = AuthInterceptor.getUID();
        Map map = baiduAIService.faceSearch(multipartFile.getBytes(), uid + "");
        ;
        return Msg.success().data(map);
    }

    @PostMapping(value = "/bank-card", consumes = {"multipart/form-data"})
    public Msg<Map> bankCard(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        System.out.println("银行卡ocr");
        Map map = baiduAIService.bankCardOcr(multipartFile.getBytes());
        return Msg.success().data(map);
    }

    @PostMapping(value = "/id-card/front", consumes = {"multipart/form-data"})
    public Msg<Map> idCardFront(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        System.out.println("身份证ocr front");
        Map map = baiduAIService.idCardOcr(multipartFile.getBytes(), "front");

        return Msg.success().data(map);
    }

    @PostMapping(value = "/id-card/back", consumes = {"multipart/form-data"})
    public Msg<Map> idCardBack(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        System.out.println("身份证ocr back");
        Map map = baiduAIService.idCardOcr(multipartFile.getBytes(), "back");
        return Msg.success().data(map);
    }
}

